using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RideMe.FindAndReplace;
using ICSharpCode.TextEditor.Document;
using RideMe.Core;

namespace RideMe.Presentation
{
    public enum SearchAndReplaceMode
    {
        Search,
        Replace
    }

    public partial class FindReplaceDialog : Form 
    {
        public event EventHandler FindAllStarted;
        public event EventHandler<SearchAllFinishedEventArgs> FindAllFinished;

		public string FindWhatText
		{
			get { return findWhatTextBox.Text; }
			set { findWhatTextBox.Text = value; }
		}

        protected virtual void OnFindAllStarted(EventArgs e)
        {
            if (FindAllStarted != null)
            {
                FindAllStarted(this, e);
            }
        }

        protected virtual void OnFindAllFinshed(SearchAllFinishedEventArgs e)
        {
            if (FindAllFinished != null)
            {
                FindAllFinished(this, e);
            }
        }

        private static readonly int ReplaceHeight = 376;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:FindReplaceForm"/> class.
        /// </summary>
        public FindReplaceDialog()
        {
            InitializeComponent();

            searchTypeComboBox.SelectedIndex = 0;
            findWhereComboBox.SelectedIndex = 0;
            findNextButton.Focus();
            
            

            HideDisplayReplace(false);

            if (ProjectService.OpenFiles.Count == 0)
            {
                findWhereComboBox.SelectedIndex = 2;
            }
        }

        private SearchAndReplaceMode _searchAndReplaceMode;

        public SearchAndReplaceMode SearchAndReplaceMode
        {
            get { return _searchAndReplaceMode; }
            set 
            {
                _searchAndReplaceMode = value;

                bool replace = _searchAndReplaceMode == SearchAndReplaceMode.Replace;

                HideDisplayReplace(replace);
                replaceToolStripButton.Checked = replace;

                ResizeForm(replace);
            }
        }

        private void ResizeForm(bool replace)
        {
            this.SuspendLayout();
            if (replace)
            {
                this.Height = ReplaceHeight;
            }
            else
            {
                this.Height = ReplaceHeight - replaceHeadingControl.Height - replaceWithTextBox.Height;
            }
            this.ResumeLayout();
        }

        public DocumentIteratorType DocumentIteratorType
        {
            get
            {
                return (DocumentIteratorType)findWhereComboBox.SelectedIndex;
            }
            set
            {
                findWhereComboBox.SelectedIndex = (int)value;
            }
        }

        static bool IsTextSelected(ISelection selection)
        {
            if (selection != null)
            {
                return !selection.IsEmpty;
            }
            return false;
        }

        private void WritebackOptions()
        {
            SearchOptions.FindPattern = findWhatTextBox.Text;

            if (_searchAndReplaceMode == SearchAndReplaceMode.Replace)
            {
                SearchOptions.ReplacePattern = replaceWithTextBox.Text;
            }

            SearchOptions.LookIn = findWhereComboBox.Text;
            SearchOptions.MatchCase = matchCaseCheckBox.Checked;
            SearchOptions.MatchWholeWord = matchWholeWordCheckBox.Checked;

            SearchOptions.SearchStrategyType = (SearchStrategyType)searchTypeComboBox.SelectedIndex;
            SearchOptions.DocumentIteratorType = this.DocumentIteratorType;
        }

        private void HideDisplayReplace(bool visible)
        {
            replaceHeadingControl.Visible = visible;
            replaceWithTextBox.Visible = visible;
            replaceButton.Visible = visible;
            replaceAllButton.Visible = visible;
        }

        private void findNextButton_Click(object sender, EventArgs e)
        {
            WritebackOptions();
            SearchReplaceManager.FindNext();
        }

        private void findAllButton_Click(object sender, EventArgs e)
        {
            WritebackOptions();
            SearchReplaceInFilesManager.SearchAllFinished += new SearchAllFinishedEventHandler(SearchReplaceInFilesManager_SearchAllFinished);
            OnFindAllStarted(EventArgs.Empty);
            SearchReplaceInFilesManager.FindAll();
        }

        void SearchReplaceInFilesManager_SearchAllFinished(object sender, SearchAllFinishedEventArgs e)
        {
            OnFindAllFinshed(e);
            SearchReplaceInFilesManager.SearchAllFinished -= new SearchAllFinishedEventHandler(SearchReplaceInFilesManager_SearchAllFinished);
        }

        private void replaceAllButton_Click(object sender, EventArgs e)
        {
            WritebackOptions();
            SearchReplaceManager.ReplaceAll();
        }

        private void replaceButton_Click(object sender, EventArgs e)
        {
            WritebackOptions();
            SearchReplaceManager.Replace();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void findToolStripButton_CheckedChanged(object sender, EventArgs e)
        {
            if (findToolStripButton.Checked)
            {
                HideDisplayReplace(false);
                replaceToolStripButton.Checked = false;
                this.SearchAndReplaceMode = SearchAndReplaceMode.Search;
            }
        }

        private void replaceToolStripButton_CheckedChanged(object sender, EventArgs e)
        {
            if (replaceToolStripButton.Checked)
            {
                HideDisplayReplace(true);
                findToolStripButton.Checked = false;
                this.SearchAndReplaceMode = SearchAndReplaceMode.Replace;
            }
       }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                this.Close();
            }
            if ( ( e.KeyData == Keys.Enter ) || ( e.KeyData == Keys.F3 ) )
            {
                if ( _searchAndReplaceMode == SearchAndReplaceMode.Search )
                {
                    findNextButton.PerformClick();
                    //findAllButton.PerformClick();
                }
                else
                {
                    replaceButton.PerformClick();
                }
            }
        }

        private void findWhereComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enable = true;

            if ( (ProjectService.OpenFiles.Count == 0) & (DocumentIteratorType == DocumentIteratorType.CurrentDocument) | (DocumentIteratorType == DocumentIteratorType.AllOpenFiles))
            {
                enable = false;
            }

            findNextButton.Enabled = enable;
        }

        private void textBox_KeyUp(object sender, KeyEventArgs e)
        {
            //if ((e.KeyData == Keys.Enter) || (e.KeyData==Keys.F3))
            //{
            //    if (_searchAndReplaceMode == SearchAndReplaceMode.Search)
            //    {
            //        findNextButton.PerformClick();
            //        //findAllButton.PerformClick();
            //    }
            //    else
            //    {
            //        replaceButton.PerformClick();
            //    }
            //}

        }

    }
}
